package com.yksj.healthtalk.ui.doctorstation.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.ui.R;

/**
 * 预约详细
 * @author zhao
 *
 */
public class DoctorQuerySubscribeServiceDetailedActivity extends
		BaseFragmentActivity implements OnClickListener{

	String mTitle;
	String mData;
	ViewGroup mViewGroup;
	JSONObject mJsonObject;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_query_subscribe_service_detail_layout);
		
		if(arg0 != null){
			mTitle = arg0.getString("mTitle");
			mData = arg0.getString("mData");
		}else{
			mTitle = getIntent().getStringExtra("title");
			mData = getIntent().getStringExtra("json");
		}
		
		initUI();
		
		onParseData(mData);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("mTitle",mTitle);
		outState.putString("mData", mData);
	}
	
	private void initUI(){
		initTitle();
		setTitle(mTitle);
		setBackBtn("返回",this);
		mViewGroup = (ViewGroup)findViewById(R.id.content);
	}
	
	/**
	 * 
	 * 解析数据
	 * @param content
	 * 
	 */
	@SuppressLint("SimpleDateFormat")
	private void onParseData(String content){
		try {
			 mJsonObject=new org.json.JSONObject(content);
			titleTextV.setText(mJsonObject.getString("sevice_name_str"));
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			//SimpleDateFormat format1 = new SimpleDateFormat("dd:HH");
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");
			if("2".equals(mJsonObject.getString("SERVICE_TYPE_ID"))){//预约时段咨询
				View view = getLayoutInflater().inflate(R.layout.subscribe_online_service_detail_layout, null);
				mViewGroup.addView(view);
				String buyCount = mJsonObject.optString("ORDER_CURRENT","0")+"人";//人数
				String price = mJsonObject.optString("SERVICE_PRICE","0")+"元";//价格
				String maxNumber = mJsonObject.optString("SERVICE_MAX","0")+"人";//最大名额
				//jsonObject.getString("SERVICE_MAX");
				String startTime = mJsonObject.getString("SERVICE_TIME_BEGIN");
//				String endTime = jsonObject.getString("SERVICE_TIME_END");
				Date date1 = format.parse(startTime);
//				Date date2 = format.parse(endTime);
//				format1.format(date1);
//				format1.format(date2);
				TextView textView  = (TextView)view.findViewById(R.id.txt1); 
				textView.setText(format2.format(date1));
				textView = (TextView)view.findViewById(R.id.txt2);
				textView.setText(mJsonObject.getString("title_time"));
				textView = (TextView)view.findViewById(R.id.txt3);
				textView.setText(maxNumber);
				textView = (TextView)view.findViewById(R.id.txt4);
				textView.setText(price);
				textView = (TextView)view.findViewById(R.id.txt5);
				textView.setText(buyCount);
				textView = (TextView)view.findViewById(R.id.txt6);
				textView.setOnClickListener(this);
				//"SERVICE_TIME_BEGIN": "20131219000000",
			    //"SERVICE_TIME_END": "20131219000400",
			}else{//面访
				View view = getLayoutInflater().inflate(R.layout.subscribe_facetoface_service_detail_layout, null);
				mViewGroup.addView(view);
				String buyCount = mJsonObject.optString("ORDER_CURRENT","0")+"人";//人数
				String price = mJsonObject.optString("SERVICE_PRICE","0")+"元";//价格
				String maxNumber = mJsonObject.optString("SERVICE_MAX","0");//最大名额
				String startTime = mJsonObject.optString("SERVICE_TIME_BEGIN");
				Date date1 = format.parse(startTime);
				TextView textView  = (TextView)view.findViewById(R.id.txt1); 
				textView.setText(format2.format(date1));
				textView = (TextView)view.findViewById(R.id.txt2);
				textView.setText(mJsonObject.getString("title_time"));
				textView = (TextView)view.findViewById(R.id.txt3);
				textView.setText(maxNumber);
				textView = (TextView)view.findViewById(R.id.txt4);
				textView.setText(price);
				textView = (TextView)view.findViewById(R.id.txt5);
				textView.setText(buyCount);
				textView = (TextView)view.findViewById(R.id.txt6);
				textView.setOnClickListener(this);
				textView = (TextView)view.findViewById(R.id.txt7);
				textView.setText(mJsonObject.getString("SERVICE_PLACE"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.txt6://购买者
			String SERVICE_ITEM_ID = mJsonObject.optString("SERVICE_ITEM_ID");
			Intent intent = new Intent(this,DoctorServiceBuyerListActivity.class);
			intent.putExtra("serviceItemId", SERVICE_ITEM_ID);
			intent.putExtra("title", mTitle);
			intent.putExtra("date", mJsonObject.optString("SERVICE_TIME_BEGIN").substring(0, 8));
			
			startActivityForResult(intent,100);
			break;
		}
	}
	
}
